﻿@namespace Masa.Blazor
@using Masa.Blazor.Extensions
@inherits MasaComponentBase

<div class="@GetClass()"
     style="@GetStyle()"
     id="@Id"
     @ref="Ref"
     @attributes="@Attributes">
    <CascadingValue Value="IsDark" Name="IsDark">
        @if (HasHeader)
        {
            <div class="@_header">
                @RenderFragments.RenderFragmentOrText(TitleContent, Title, _headerTitle.Name)
                @RenderFragments.RenderIfNotNull(ActionsContent, _headerActions.Name)
            </div>
        }

        <div class="@_block.Extend("view")">
            <table>
                <tbody>
                @foreach (var row in Rows)
                {
                    @if (Vertical)
                    {
                        <tr class="@_block.Extend("row")">
                            @foreach (var cell in row)
                            {
                                @if (Bordered)
                                {
                                    <th class="@_itemContainerLabel.AppendClasses(LabelClass, cell.LabelClass)"
                                        style="@GetStyle(LabelStyle, cell.LabelStyle)"
                                        colspan="@cell.Span">
                                        @cell.Label
                                    </th>
                                }
                                else
                                {
                                    <th class="@_item.Name" colspan="@cell.Span">
                                        <div class="@_itemContainer.Name">
                                            <span class="@_itemContainerLabelModifierBuilder.Add("no-colon", !Colon).AddClass(LabelClass, cell.LabelClass)"
                                                  style="@GetStyle(LabelStyle, cell.LabelStyle)">
                                                @cell.Label
                                            </span>
                                        </div>
                                    </th>
                                }
                            }
                        </tr>
                        <tr class="@_block.Extend("row")">
                            @foreach (var cell in row)
                            {
                                @if (Bordered)
                                {
                                    <td class="@_itemContent.AppendClasses(ContentClass, cell.Class)"
                                        style="@GetStyle(ContentStyle, cell.Style)"
                                        colspan="@cell.Span">
                                        @cell.ChildContent
                                    </td>
                                }
                                else
                                {
                                    <td class="@_item.Name" colspan="@cell.Span">
                                        <div class="@_itemContainer.Name">
                                            <span class="@_itemContainerContent.AppendClasses(ContentClass, @cell.Class)"
                                                  style="@GetStyle(ContentStyle, cell.Style)">
                                                @cell.ChildContent
                                            </span>
                                        </div>
                                    </td>
                                }
                            }
                        </tr>
                    }
                    else
                    {
                        <tr class="@_row.Name">
                            @foreach (var cell in row)
                            {
                                @if (Bordered)
                                {
                                    var thSpan = 1;
                                    var tdSpan = cell.Span * 2 - 1;

                                    <th class="@_itemLabel.AppendClasses(LabelClass, cell.LabelClass)"
                                        style="@GetStyle(LabelStyle, cell.LabelStyle)"
                                        colspan="@thSpan">
                                        @cell.Label
                                    </th>
                                    <td class="@_itemContent.AppendClasses(ContentClass, cell.Class)"
                                        style="@GetStyle(ContentStyle, cell.Style)"
                                        colspan="@tdSpan">
                                        @cell.ChildContent
                                    </td>
                                }
                                else
                                {
                                    <td class="@_item.Name" colspan="@cell.Span">
                                        <div class="@_itemContainer">
                                            <span class="@_itemContainerLabelModifierBuilder.Add("no-colon", !Colon).AddClass(LabelClass, cell.LabelClass)"
                                                  style="@GetStyle(LabelStyle, cell.LabelStyle)">
                                                @cell.Label
                                            </span>
                                            <span class="@_itemContainerContent.AppendClasses(ContentClass, cell.Class)"
                                                  style="@GetStyle(ContentStyle, cell.Style)">
                                                @cell.ChildContent
                                            </span>
                                        </div>
                                    </td>
                                }
                            }
                        </tr>
                    }
                }
                </tbody>
            </table>
        </div>
    </CascadingValue>

    <CascadingValue Value="this" IsFixed>
        @ChildContent
    </CascadingValue>
</div>

